package com.ajie.slimming.util;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.ajie.slimming.config.Constants;
import com.ajie.slimming.ui.SlimmingApplication;

public class DataManager
{
    //起始符   模式  力度  时间  间隔符 模式  力度  时间   结束符
    //String data = "fd 03 01 05 fe 02 01 03 fe 01 01 10 fe 01 02 55 ff";
    private static final String DATA_KEY = "src_data";
    
    private static Context context = SlimmingApplication.getBPContext();
    
    public boolean hasData()
    {
        return check(getData());
    }
    
    public boolean saveData(String newData)
    {
        SharedPreferences preferences = context.getSharedPreferences(Constants.PRE_SRC_DATA,
                Activity.MODE_PRIVATE);
        Editor editor = preferences.edit();
        
        /************* for test ***************************************/
                String data = "fd 03 01 05 fe 02 01 03 fe 01 01 10 fe 01 02 55 ff";
                editor.putString(DATA_KEY, data);
        /************* for test ***************************************/
        /****************************************************/
//        String currentData = getData();
//        if (TextUtils.isEmpty(currentData))
//        {
//            editor.putString(DATA_KEY, newData);
//        }
//        else
//        {
//            //合并新老数据
//            if (check(newData) && check(currentData))
//            {
//                String result = combine(currentData, newData);
//                if (TextUtils.isEmpty(result))
//                {
//                    return false;
//                }
//                else0
//                {
//                    editor.putString(DATA_KEY, result);
//                }
//            }
//            else
//            {
//                return false;
//            }
//        }
        /*******************************************************/
        return editor.commit();
    }
    
    private static String combine(String oldData, String newData)
    {
        return oldData.substring(0, oldData.length() - 3) + " fe "
                + newData.substring(3, newData.length());
    }
    
    private static String getData()
    {
        SharedPreferences preferences = context.getSharedPreferences(Constants.PRE_SRC_DATA,
                Activity.MODE_PRIVATE);
        return preferences.getString(DATA_KEY, null);
    }
    
    /** 获取总共消耗的热量值 */
    public String getTotalCalorie()
    {
        double resultValue = 0;
        String data = getData();
        if (check(data))
        {
            data = data.substring(3, data.length() - 3);
            String groups[] = data.split("fe");
            for (String group : groups)
            {
                group = group.trim();
                String attrs[] = group.split(" ");
                resultValue += Util.getHeatCalorie(attrs[1], attrs[2]);
            }
            System.out.println("get total:" + resultValue);
            return String.format(Locale.getDefault(), "%.0f", resultValue);
        }
        return null;
    }
    
    /** 获取最后一次消耗的热量值 */
    public String getLatestCalorie()
    {
        String data = getData();
        if (check(data))
        {
            data = data.substring(3, data.length() - 3);
            String groups[] = data.split("fe");
            String group = groups[groups.length - 1].trim();
            String attrs[] = group.split(" ");
            double resultValue = Util.getHeatCalorie(attrs[1], attrs[2]);
            return String.format(Locale.getDefault(), "%.0f", resultValue);
        }
        return null;
    }
    
    private static boolean check(final String data)
    {
        if (TextUtils.isEmpty(data))
        {
            return false;
        }
        if (!(data.startsWith("fd ") && data.endsWith(" ff")))
        {
            return false;
        }
        String middle = data.substring(3, data.length() - 3);
        String groups[] = middle.split("fe");
        for (String str : groups)
        {
            str = str.trim();
            String attrs[] = str.split(" ");
            if (attrs.length != 3)
            {
                return false;
            }
        }
        return true;
    }
}
